package com.tina.controller;

import com.tina.entity.Employee;
import com.tina.service.EmployeeService;
import com.tina.utils.ResultFormat;
import com.tina.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author Tina
 * @version 0.0.1
 * @date 2020/7/1 22:15
 **/
@Controller
@Validated
public class EmployeeController {

    @Autowired
    private EmployeeService service;

    /**
     * 查询员工信息
     *
     * @return
     */
    @GetMapping("/empList")
    @ResponseBody
    public ResultFormat empList() {
        List<Employee> employees = service.empList();
        if (employees != null) {
            return ResultUtil.success(employees);
        }
        return ResultUtil.success(100, "暂时无数据");
    }

    /**
     * 新增员工
     *
     * @return
     */
    @PostMapping("/saveEmp")
    @ResponseBody
    public ResultFormat saveEmp(@Valid Employee employee) {
        Integer saveEmp = service.saveEmp(employee);
        if (saveEmp > 0) {
            return ResultUtil.success();
        }
        return ResultUtil.error();
    }

    /**
     * 编辑员工
     *
     * @return
     */
    @PutMapping("/editEmp")
    @ResponseBody
    public ResultFormat editEmp(@Valid Employee employee) {
        Integer editEmp = service.editEmp(employee);
        if (editEmp > 0) {
            return ResultUtil.success();
        }
        return ResultUtil.error();
    }

    /**
     * 删除员工
     *
     * @return
     */
    @DeleteMapping("/delEmp")
    @ResponseBody
    public ResultFormat delEmp(Integer empId) {
        if (empId == null) {
            return ResultUtil.error(100, "ID不能为空");
        }
        {
            Integer delEmp = service.delEmp(empId);
            if (delEmp > 0) {
                return ResultUtil.success();
            }
            return ResultUtil.error();
        }
    }

}
